<?php

/**
 * 
 * @project     : innoTweets 1.0
 * @author      : Dino (DBK)
 * 
 * @package     : Tweet
 * @description : Tweet for mobile
 * 
 */
if (!defined('BASEPATH')) {
  exit('No direct script access allowed');
}

class Tweet extends MOBILE_AUTH_Controller {

  public function index() {

    // Getting & Cleaning inputs
    $tweet_id = (int) $this->input->get('tweet_id', TRUE);

    // Checking tweet id
    if (!empty($tweet_id) && is_numeric($tweet_id)) {

      // Getting tweet data
      $get_tweet_by_id = $this->tweet_model->get_tweet_by_id($tweet_id);

      // Checking tweet data
      if ($get_tweet_by_id['status'] == 1 && !empty($get_tweet_by_id['result'])) {

        $tweet = $this->make_tweet_response($get_tweet_by_id['result']);

        // Getting tweet users
        $get_tweet_users = $this->tweet_model->get_tweet_users($tweet_id);
        $repliers = $this->make_users_response($get_tweet_users['result']);

        // Getting tweet extras
        $get_tweet_extras = $this->tweet_model->get_tweet_extras($tweet_id, $this->mobile_current_user['user_id']);
        $extras = $this->make_extras_response($get_tweet_extras['result']);

        // Getting tweet followers
        $get_followers = $this->tweet_model->get_followers($tweet_id);
        $followers = $this->make_users_response($get_followers['result']);

        // Getting tweet viewers
        $tweet_viewers = @tweet_viewers($tweet_id);
        $viewers = $this->make_users_response($tweet_viewers);

        $this->response['status'] = 1;
        $this->response['message'] = 'Success';
        $this->response['data'] = array(
            'tweet' => $tweet,
            'extras' => $extras,
            'repliers' => $repliers,
            'followers' => $followers,
            'viewers' => $viewers
        );
      } else {
        $this->response['message'] = 'Tweet not exists';
      }
    } else {
      $this->response['message'] = 'Invalid tweet id';
    }
  }

  /**
   * Get replies of a tweet
   * 
   */
  public function replies() {

    // Getting & Cleaning inputs
    $tweet_id = (int) $this->input->get('tweet_id', TRUE);
    $reply_limit = (int) $this->input->get('limit', TRUE);
    $reply_limit = (int) (!empty($reply_limit)) ? $reply_limit : 10;
    $reply_sort = (int) $this->input->get('sort', TRUE);

    if (!empty($tweet_id) && is_numeric($tweet_id)) {

      // Getting tweet replies
      $get_tweet_reply_by_id = $this->tweet_model->get_tweet_reply_by_id($tweet_id, $reply_limit, $reply_sort);

      if ($get_tweet_reply_by_id['status'] == 1 && !empty($get_tweet_reply_by_id['result'])) {

        $this->response['status'] = 1;
        $this->response['message'] = 'Success';
        $this->response['data'] = array('replies' => $this->make_replies_response($get_tweet_reply_by_id['result']));
      } else {
        $this->response['message'] = 'No replies';
      }
    } else {
      $this->response['message'] = 'Invalid tweet id';
    }
  }

  /**
   * Create a new tweet
   * 
   */
  public function create() {

    // Checking request type is post
    if ($this->is_post()) {

      // Cleaning inputs
      $tweet_subject = $this->sanitize($this->input->post('subject', TRUE));
      $tweet_message = $this->sanitize($this->input->post('message', TRUE));

      // Checking inputs
      if (!empty($tweet_subject) && !empty($tweet_message)) {

        // Inserting tweet
        $set_tweet = $this->tweet_model->set_tweet($tweet_subject, $tweet_message, $this->mobile_current_user['user_id']);

        // Checking insert status
        if ($set_tweet['status'] == 1 && !empty($set_tweet['result'])) {

          // Set the owner as follower of tweet
          $this->tweet_model->add_follow($set_tweet['result']['tweet_id'], $this->mobile_current_user['user_id']);

          $this->response['status'] = 1;
          $this->response['message'] = 'Success';
          $this->response['data'] = array('tweet' => array('tweet_id' => $set_tweet['result']['tweet_id']));
        }
      } else {
        $this->response['message'] = 'Tweet subject and message required';
      }
    } else {
      $this->response['message'] = 'Invalid request';
    }
  }

  /**
   * Update a tweet (Edit)
   * 
   */
  public function update() {

    if ($this->is_post()) {

      // Getting & Cleaning inputs
      $tweet_id = (int) $this->input->get('tweet_id', TRUE);
      $tweet_subject = $this->sanitize($this->input->post('subject', TRUE));
      $tweet_message = $this->sanitize($this->input->post('message', TRUE));

      // Checking tweet id
      if (!empty($tweet_id) && is_numeric($tweet_id)) {

        // Getting tweet
        $get_tweet_by_id = $this->tweet_model->get_tweet_by_id($tweet_id);

        // Checking tweet
        if ($get_tweet_by_id['status'] == 1 && !empty($get_tweet_by_id['result'])) {

          // Checking tweet ownership
          if ($get_tweet_by_id['result']['tweet_user_id'] == $this->mobile_current_user['user_id']) {

            // Checking inputs
            if (!empty($tweet_subject) && !empty($tweet_message)) {

              // Updating tweet
              $update_tweet_by_id = $this->tweet_model->update_tweet_by_id($tweet_id, $tweet_subject, $tweet_message);

              // Checking update
              if ($update_tweet_by_id['status'] == 1 && !empty($update_tweet_by_id['result'])) {

                $this->response['status'] = 1;
                $this->response['message'] = 'Success';
                $this->response['data'] = array('tweet' => array('tweet_id' => $tweet_id));
              } else {
                $this->response['message'] = 'Tweet update failed or no changes made';
              }
            } else {
              $this->response['message'] = 'Tweet subject and message required';
            }
          } else {
            $this->response['message'] = 'Permission denied';
          }
        } else {
          $this->response['message'] = 'Tweet not exitsts';
        }
      } else {
        $this->response['message'] = 'Invalid tweet id';
      }
    } else {
      $this->response['message'] = 'Invalid request';
    }
  }

  /**
   * Delete a tweet
   * 
   */
  public function delete() {

    if ($this->is_post()) {

      // Getting inputs
      $tweet_id = (int) $this->input->post('tweet_id', TRUE);

      // Checking tweet id
      if (!empty($tweet_id) && is_numeric($tweet_id)) {

        $del_tweet_by_id = $this->tweet_model->del_tweet_by_id($tweet_id, $this->mobile_current_user['user_id']);

        // Deleting tweet (Setting status to 0)
        if ($del_tweet_by_id['status'] == 1 && !empty($del_tweet_by_id['result'])) {

          $this->response['status'] = 1;
          $this->response['message'] = 'Success';
          $this->response['data'] = array('tweet' => array('tweet_id' => $tweet_id));
        } else {
          $this->response['message'] = 'Deleting failed';
        }
      } else {
        $this->response['message'] = 'Invalid tweet id';
      }
    } else {
      $this->response['message'] = 'Invalid request';
    }
  }

  /**
   * Preparing responses
   * 
   */
  private function make_tweet_response($tweet = array()) {
    $oneTweet = array();
    if (!empty($tweet)) {
      $like_data = get_user_like_status('tweet', $tweet['tweet_id']);
      $get_follow = $this->tweet_model->get_follow($tweet['tweet_id'], $this->mobile_current_user['user_id']);
      $oneTweet = array();
      $oneTweet['tweet_id'] = (int) $tweet['tweet_id'];
      $oneTweet['tweet_subject'] = $tweet['tweet_subject'];
      $oneTweet['tweet_message'] = $tweet['tweet_message'];
      $oneTweet['tweet_date'] = $tweet['tweet_date'];
      $oneTweet['tweet_likes_count'] = $like_data['total_count'];
      $oneTweet['tweet_user_liked'] = $like_data['status'];
      $oneTweet['tweet_user_followed'] = (!empty($get_follow['result']) ? 'yes' : 'no');
      $oneTweet['user_id'] = $tweet['user_id'];
      $oneTweet['user_name'] = $tweet['user_name'];
      $oneTweet['user_display_name'] = $tweet['user_display_name'];
      $oneTweet['user_profile_url'] = site_url('profile/' . $tweet['user_name']);
      $oneTweet['user_image_url'] = site_url('assets/photos/' . $tweet['user_image']);
    }
    return $oneTweet;
  }

  private function make_users_response($users_data = array()) {
    $users = array();
    if (!empty($users_data)) {
      foreach ($users_data as $user) {
        $user_details = array();
        $user_details['user_id'] = (int) $user['user_id'];
        $user_details['user_name'] = $user['user_name'];
        $user_details['user_display_name'] = $user['user_display_name'];
        $user_details['user_profile_url'] = site_url('profile/' . $user['user_name']);
        $user_details['user_image_url'] = site_url('assets/photos/' . $user['user_image']);
        if (isset($user['user_replies_count'])) {
          $user_details['user_replies_count'] = (int) $user['user_replies_count'];
        }
        $users[] = $user_details;
      }
    }
    return $users;
  }

  private function make_extras_response($extras_data = array()) {
    $extras = array();
    if (!empty($extras_data)) {
      $extras = array();
      $extras['total_replies_count'] = (int) $extras_data['tweet_total_replies_count'];
      $extras['user_replies_count'] = (int) $extras_data['tweet_my_replies_count'];
    }
    return $extras;
  }

  private function make_replies_response($reply_data = array()) {
    $replies = array();
    if (!empty($reply_data)) {
      foreach ($reply_data as $reply) {

        $like_data = get_user_like_status('reply', $reply['tweet_reply_id']);

        $oneReply = array();
        $oneReply['tweet_id'] = $reply['tweet_reply_tweet_id'];
        $oneReply['reply_id'] = $reply['tweet_reply_id'];
        $oneReply['reply_message'] = $reply['tweet_reply_message'];
        $oneReply['reply_date'] = $reply['tweet_reply_date'];
        $oneReply['user_id'] = $reply['user_id'];
        $oneReply['user_name'] = $reply['user_name'];
        $oneReply['user_display_name'] = $reply['user_display_name'];
        $oneReply['user_profile_url'] = site_url('profile/' . $reply['user_name']);
        $oneReply['user_image_url'] = site_url('assets/photos/' . $reply['user_image']);

        $replies[] = $oneReply;
      }
    }
    return $replies;
  }

  /**
   * Responding Json
   * 
   */
  public function __destruct() {
    echo json_encode($this->response);
  }

}

?>